﻿#region Copyright (c) 2010 R. Uittenbosch
/*
* Copyright (C) 2010 R. Uittenbosch
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
* associated documentation files (the "Software"), to deal in the Software without restriction, including 
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
* copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
* following conditions:
* 
* The above copyright notice and this permission notice shall be included in all copies or substantial 
* portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 
* EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ServiceLocation;

namespace LinkedService.Mobile
{

  /// <summary>
  /// A straight forward servicelocator
  /// </summary>
  public class BasicServiceLocator : ServiceLocatorImplBase
  {
    /// <summary>
    /// A dictionary to hold all instanciated services
    /// </summary>
    private Dictionary<string, ServiceContainerItem> _storage;

    /// <summary>
    /// Constructor
    /// </summary>
    public BasicServiceLocator()
    {
      // Create the storage
      _storage = new Dictionary<string, ServiceContainerItem>();
    }
   
    /// <summary>
    /// Add a new component.
    /// </summary>
    /// <typeparam name="I">Interface type of the required object</typeparam>
    /// <typeparam name="T">Instance type of the required object</typeparam>
    /// <param name="key">A key to locate it</param>
    /// <param name="instance">An instance of the required object</param>
    public void AddComponent<I, T>(string key, object instance) where T : class
    {
      //Add it to the storage
      _storage.Add(key, new ServiceContainerItem() 
                  { Instance = instance, 
                    InstanceType = typeof(T),
                    InterfaceType = typeof(I) 
                  });
    }
    
    /// <summary>
    /// Get a list of all object of the desired type
    /// </summary>
    /// <param name="serviceType">The type of object required</param>
    /// <returns></returns>
    protected override IEnumerable<object> DoGetAllInstances(Type serviceType)
    {
      List<object> l = new List<object>();
      foreach (ServiceContainerItem i in _storage.Values)
      {
        // If the save, add it to the list
        if (serviceType.ToString() == i.InterfaceType.ToString())
         l.Add(i.Instance);
      }
      return l.ToArray();
    }

    /// <summary>
    /// Get a specific instance of the required type
    /// </summary>
    /// <param name="serviceType">Type of object</param>
    /// <param name="key">Key to find it with</param>
    /// <returns></returns>
    protected override object DoGetInstance(Type serviceType, string key)
    {
      if (string.IsNullOrEmpty(key))
      {
        key = serviceType.ToString();
      }
      return ((ServiceContainerItem)_storage[key]).Instance;
    }
  }
}